/*
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package timers;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Timer;
import java.util.TimerTask;

import main.Bot;
import main.CommonMethods;
import main.Main;
import main.UserCentral;

import org.apache.log4j.Logger;
import org.jibble.pircbot.Colors;

import database.DBConnectionFactory;

/**
 * A timer that represents an army that is out
 * 
 * 
 */
public class ArmyTimer extends Timer {
   /**
    * A Class that represents a task to be done when the timer runs out
    * 
    * 
    */
   private static class ToDoTask extends TimerTask {
	  private final String user;
	  private final Integer army;

	  public ToDoTask(final String user, final Integer army) {
		 super();
		 this.user = user;
		 this.army = army;
	  }

	  @Override
	  public void run() {
		 if (!user.startsWith("prov ")) {
			for (final String s : UserCentral.getInstance().getCurrentNicks(user)) {
			   Bot.getInstance().sendMessage(s, s + ": army " + army + " has returned!");
			}
			Bot.getInstance().sendMessage(Main.getBotSettings().getMainChannel(),
				  Colors.BLUE + user + "'s " + Colors.TEAL + "army " + army + Colors.OLIVE + " has returned!");
		 } else {
			Bot.getInstance().sendMessage(Main.getBotSettings().getMainChannel(),
				  Colors.BLUE + user.substring(5) + "'s " + Colors.TEAL + "army " + army + Colors.OLIVE + " has returned!");
		 }

		 Connection con = DBConnectionFactory.getConnectionHandler().getConnection();
		 PreparedStatement ps = null;
		 try {
			if (!user.startsWith("prov "))
			   ps = con.prepareStatement("DELETE FROM army WHERE " + "mainnick = ? AND armyno = ?");
			else
			   ps = con.prepareStatement("DELETE FROM somarmy WHERE " + "name = ? AND armyno = ?");

			if (!user.startsWith("prov "))
			   ps.setString(1, user);
			else
			   ps.setString(1, user.substring(5));
			ps.setInt(2, army);
			ps.executeUpdate();
			Bot.getInstance().getTimerHandler().removeArmyTimer(user, army);
		 } catch (final SQLException e) {
			Logger.getLogger("Error").error("", e);
		 } finally {
			try {
			   ps.close();
			} catch (final Exception e) {
			   Logger.getLogger("Error").error("", e);
			}
		 }
		 CommonMethods.doSMSNotification(user, 0, "");
	  }
   }

   private final Integer army;

   private final String user;

   public ArmyTimer(final String user, final Integer army, final Long delay) {
	  super();
	  this.army = army;
	  this.user = user;
	  this.schedule(new ToDoTask(user, army), delay);
   }

   public Integer getArmy() {
	  return army;
   }

   public String getUser() {
	  return user;
   }
}
